/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.    
*/

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DBConnection;

namespace Logger
{
    /// <summary>
    /// 
    /// </summary>
    public class TransactionLog
    {
        /// <summary>
        /// 
        /// </summary>
        protected static string strLogFilePath = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private static StreamWriter sw = null;

        /// <summary>
        /// Setting LogFile path. If the logfile path is null then it will update error info into LogFile.txt under
        /// application directory.
        /// </summary>
        public static string LogFilePath
        {
            set
            {
                strLogFilePath = value;
            }
            get
            {
                return strLogFilePath;
            }
        }

         /// <summary>
        /// Empty Constructor
        /// </summary>
        public TransactionLog() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTransaction"></param>
        /// <returns></returns>
        public static bool WriteToTransactionFile(string strTransaction)
        {
           // string strPathName = string.Empty;
            bool bReturn = true;

            //If the log file path is not empty but the file is not available it will create it
            if (false == File.Exists(strLogFilePath))
            {
                FileStream fs = new FileStream(strLogFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                fs.Close();
            }

            if (true != WriteTransactionLog(strLogFilePath, strTransaction))
            {
                bReturn = false;
            }
            return bReturn;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strPathName"></param>
        /// <param name="strTransaction"></param>
        /// <returns></returns>
        private static bool WriteTransactionLog(string strPathName, string strTransaction)
        {
          //  #if DEBUG

            try
            {
                sw = new StreamWriter(strPathName, true);
                sw.WriteLine("Transaction		: " + strTransaction.Trim());
                sw.WriteLine("Date		: " + DateTime.Now.ToLongTimeString());
                sw.WriteLine("Time		: " + DateTime.Now.ToShortDateString());
                sw.WriteLine("^^-------------------------------------------------------------------^^");
                sw.Flush();
                sw.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

           // #endif
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTransaction"></param>
        public static void HandleTransaction(string strTransaction)
        {
            if (TransactionLog.strLogFilePath != null)
            {
                if (!TransactionLog.WriteToTransactionFile(strTransaction))
                {
                    CMsgBox.ShowMsgBox("Unable to write a log");
                }
            }
        }


        /// <summary>
        /// Resets Log Files back to 0 bytes
        /// </summary>
        public static void ResetLogFile()
        {
            if (File.Exists(LogFilePath))
            {
                File.Delete(LogFilePath);
            }
        }
    }
}
